Omit<T, K extends keyof T>
Pick<T, K extends keyof T>の逆
Kに指定したものを除外する
例
code:ts
type A = Omit<{ a: string; b: string }, 'a'>; // {b: string}
実際の定義
code:ts
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
なぜかK extends keyof any
K extends keyof Tだとなにか問題がある #??
他の定義例
code:ts
type Omit<T extends object, K extends keyof T> = {
key in Exclude<keyof T, K>: Tkey;
};
こっちの定義のほうが自然な気がするmrsekut.icon
TypeScriptのRecordからRecordへの型レベルfilterするなら
code:ts
type Omit<T, K extends keyof T> = {
Key in keyof T as Key extends K ? never : Key: TKey;
};